<template>
  <div>
    <div class="head" style="width: 1200px">
      <el-form
        :inline="true"
        :model="form"
        class="demo-form-inline"
        @submit.native.prevent
      >
        <el-form-item label="用户名称">
          <el-input
            v-model="form.uname"
            placeholder="输入用户名称:模糊查询"
            @keyup.native.enter="onSubmit"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>

      <el-button
        type="primary"
        @click="dialogFormVisible = true"
        style="margin-right: 150px"
        >新增用户</el-button
      >
      <el-dialog
        title="新增用户"
        :visible.sync="dialogFormVisible"
        destroy-on-close="true"
      >
        <el-form
          ref="form"
          :rules="rules"
          :model="form"
          label-width="100px"
          style="width: 500px"
        >
          <el-form-item label="用户账号" prop="user">
            <el-input v-model="form.user" />
          </el-form-item>
          <el-form-item prop="uname" label="用户姓名">
            <el-input v-model="form.uname" />
          </el-form-item>
          <el-form-item prop="phone" label="手机号">
            <el-input v-model="form.phone" />
          </el-form-item>
          <el-form-item prop="pwd" label="密码">
            <el-input type="password" v-model="form.pwd" />
          </el-form-item>
          <el-form-item prop="checkPass" label="确认密码">
            <el-input type="password" v-model="form.checkPass" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </div>
      </el-dialog>
    </div>

    <el-divider></el-divider>

    <el-table :data="users" style="margin: 0 15px">
      <el-table-column label="编号" prop="userid"></el-table-column>
      <el-table-column label="账号" prop="user"></el-table-column>
      <el-table-column label="姓名" prop="uname"></el-table-column>
      <el-table-column label="手机号" prop="phone"></el-table-column>
      <el-table-column label="操作" width="180px">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="warning"
            icon="el-icon-edit"
            circle
            title="修改"
            @click="$router.push(`/home/user-update/${scope.row.userid}`)"
          ></el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            circle
            title="删除"
            @click="delItem(scope.row.user)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <el-pagination
      background
      layout="->,prev, pager, next,total,jumper"
      :current-page="this.page"
      :page-size="this.pagesize"
      :total="this.total"
      @current-change="pageChange"
      style="margin-top: 30px"
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogFormVisible: false,
      keyword: '',
      form: {
        uname: '', // 绑定模糊查询的姓名
        user: '',
        phone: '',
        pwd: '',
        checkPass: '',
      },
      users: [],
      page: 1,
      pagesize: 5,
      total: 0,
      rules: {
        uname: [
          { required: true, message: '请输用户名', trigger: 'blur' },
          {
            pattern: /^.{2,16}$/,
            message: '用户名应为2~16个字符',
            trigger: 'blur',
          },
        ],
        user: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          {
            pattern: /^[A-Za-z0-9]{6,16}$/,
            message: '账号应为数字、英文组成的6~16个字符',
            trigger: 'blur',
          },
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号格式错误',
            trigger: 'blur',
          },
        ],
        pwd: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            pattern:
              /(?!^(\d+|[a-zA-Z]+|[~!@#$%^&*()_.]+)$)^[\w~!@#$%^&*()_.]{6,16}$/,
            message: '6-16位字符,为字母、数字、特殊符号，至少两种组合',
            trigger: 'blur',
          },
        ],
        checkPass: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (value == this.form.pwd) {
                callback()
              }
              callback(new Error('两次密码输入不一致'))
            },
            trigger: 'blur',
          },
        ],
      },
    }
  },
  mounted() {
    let params = {
      page: this.page,
      pagesize: this.pagesize,
    }
    this.$http.userApi.list(params).then(res => {
      console.log(res)
      this.users = res.data.data
      this.total = res.data.total
    })
  },

  methods: {
    delItem(user) {
      console.log('删除的用户', user)
      let params = { user }
      this.$http.userApi.shanchu(params).then(res => {
        console.log(res)
        this.$router.go(0)
      })
    },
    submit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          let params = {
            user: this.form.user,
            uname: this.form.uname,
            phone: this.form.phone,
            pwd: this.form.pwd,
          }
          this.$http.userApi.add(params).then(res => {
            console.log(res)
            if (res.data.code == 200) {
              // 重置
              this.$refs['form'].resetFields()
              this.$message({
                message: '添加成功',
                type: 'success',
              })
              this.dialogFormVisible = false
              let params = {
                page: this.page,
                pagesize: this.pagesize,
              }
              this.$http.userApi.list(params).then(res => {
                console.log(res)
                this.users = res.data.data
                this.total = res.data.total
              })
            }
          })
        }
      })
    },
    onSubmit() {
      this.keyword = this.form.uname
      if (this.form.uname) {
        let params = {
          uname: this.form.uname,
          page: 1,
          pagesize: this.pagesize,
        }
        this.$http.userApi.mohu(params).then(res => {
          console.log('131', res)
          this.users = res.data.data
          this.total = res.data.total
        })
      } else {
        let params = {
          page: this.page,
          pagesize: this.pagesize,
        }
        this.$http.userApi.list(params).then(res => {
          console.log(res)
          this.users = res.data.data
          this.total = res.data.total
        })
      }
    },

    // 分页
    pageChange(page) {
      console.log(page)
      let params = { page: page, pagesize: this.pagesize }
      this.$http.userApi.list(params).then(res => {
        console.log(res)
        this.users = res.data.data
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.el-divider {
  margin: 24px 0 20px 0;
}
.head {
  display: flex;
  justify-content: space-between;
  height: 40px;
}
</style>
